package  com.kerrykidz.system.util.createWord;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;







import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.PropertiesUtil;
import com.kerrykidz.system.util.createWord.model.MMHeTongwordModel;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class CreateWd2003 {
	//目标word路径
	private static String appurtenancepath = "C:\\kerrykidz\\parhetong";//SystemConfigHelper.getMailPrintPath();
	
	/**
	 * 
	  * @Title: createWord2003
	  * @Description: TODO(生成word文件的路径地址)
	  * @param dataMap
	  * @param type
	  * @return
	  * @return String
	 */
	public static String createWord2003(Map<String,String> dataMap, String type,boolean isEmail){
		
		//防止报错.默认值
		dataMap.put("BBB", "");	
		dataMap.put("CCC", "");
		dataMap.put("DDD", "");
		dataMap.put("EEE", "");
		dataMap.put("FFF", "");
		dataMap.put("HHH", "");
		dataMap.put("LLL", "");
		dataMap.put("GGG", "");
		dataMap.put("NNN", "");
		dataMap.put("MMM", "");
		dataMap.put("SSS", "");
		dataMap.put("WWW", "");
		dataMap.put("RRR", "");
		dataMap.put("YYY", "");
		
		String wdFileName = "" ;//生成文件名
		String templateName = "";
		String pathFile = "";//路径 + 文件名
		
		if(null != type && "parHetong".equals(type)){ //预约打印合同 模版
			templateName = "KerryKidz-parHetong.xml";
			String pkParReserId  =dataMap.get("pkParReserId");
			wdFileName="KerryKidz-parHetong_"+pkParReserId+".doc";
		} 
		
		String dir;		// 模板路径
		try {
			dir = CreateWd2003.class.getResource("/").toURI().getPath();
			   dir +=PropertiesUtil.readValueFromReport("CREATE_WORD2003_TEMP_PATH");
			   System.out.println(dir);
			Configuration cfg = new Configuration();
			cfg.setDefaultEncoding("UTF-8");  
			cfg.setDirectoryForTemplateLoading(new File(dir));	// 加载freemarker模板文件
			cfg.setObjectWrapper(new DefaultObjectWrapper());		// 设置对象包装器
			cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);// 设计异常处理器
			
			// 获取指定模板文件
			Template template = cfg.getTemplate(templateName,"UTF-8");
			// 定义输入文件，默认生成在工程根目录
			File file = new File(appurtenancepath);
			if(!file.exists()){
				file.mkdir();
			}
			
			pathFile = appurtenancepath + File.separator + wdFileName;
			Writer out = new OutputStreamWriter(new FileOutputStream(pathFile),"UTF-8");
			template.process(dataMap, out);	// 最后开始生成
			
			out.flush();
			out.close();
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return "";//打印失败，mailprint目录不存在，或此目录在服务环境中没有赋读写执行权限，请联系运维配
		}
		return pathFile;
	}
	 
	/**
	 * 
	  * @Title: createWord2003
	  * @Description: TODO(生成word文件的路径地址)
	  * @param dataMap
	  * @param type
	  * @return
	  * @return String
	 */
	public static String createMmHetongWord2003(Map<String,String> dataMap, String type,boolean isEmail){
		
		//防止报错.默认值
		dataMap.put("BBB", "");	
		dataMap.put("CCC", "");
		dataMap.put("DDD", "");
		dataMap.put("EEE", "");
		dataMap.put("FFF", "");
		dataMap.put("HHH", "");
		dataMap.put("LLL", "");
		dataMap.put("GGG", "");
		dataMap.put("NNN", "");
		dataMap.put("MMM", "");
		dataMap.put("SSS", "");
		dataMap.put("WWW", "");
		dataMap.put("RRR", "");
		dataMap.put("YYY", "");
		
		String wdFileName = "" ;//生成文件名
		String templateName = "";
		String pathFile = "";//路径 + 文件名
		
		 
			templateName = "KerryKidz-memHetong.xml";
			String memberId  =dataMap.get("memberId");
			wdFileName="KerryKidz-memHetong_"+memberId+".doc";
		 
		
		String dir;		// 模板路径
		try {
			dir = CreateWd2003.class.getResource("/").toURI().getPath();
			   dir +=PropertiesUtil.readValueFromReport("CREATE_WORD2003_TEMP_PATH");
			   System.out.println(dir);
			Configuration cfg = new Configuration();
			cfg.setDefaultEncoding("UTF-8");  
			cfg.setDirectoryForTemplateLoading(new File(dir));	// 加载freemarker模板文件
			cfg.setObjectWrapper(new DefaultObjectWrapper());		// 设置对象包装器
			cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);// 设计异常处理器
			
			// 获取指定模板文件
			Template template = cfg.getTemplate(templateName,"UTF-8");
			// 定义输入文件，默认生成在工程根目录
			File file = new File(appurtenancepath);
			if(!file.exists()){
				file.mkdir();
			}
			
			pathFile = appurtenancepath + File.separator + wdFileName;
			Writer out = new OutputStreamWriter(new FileOutputStream(pathFile),"UTF-8");
			template.process(dataMap, out);	// 最后开始生成
			
			out.flush();
			out.close();
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return "";//打印失败，mailprint目录不存在，或此目录在服务环境中没有赋读写执行权限，请联系运维配
		}
		return pathFile;
	}
	public static void main(String[] args) throws Exception {
		Map<String, String> data = new HashMap<String, String>();
		data.put("memberId", "TJHY1410270002");					 
		data.put("mmName", "王小于");					 
		data.put("mmSex", "男");					 
		data.put("mmAge", "5");					 
		data.put("mmBirthDay", "2005-02-02");					 
		data.put("mmNationality", "中国香港");					 
		data.put("mmParentName", "王大志");					 
		data.put("mmMobile", "18888888888");					 
		data.put("mmHomeTel", "02188888888");					 
		data.put("mmEmail", "23@www.como");					 
		data.put("mmReAddress", "北京地铁站附加的总公司是立交桥23号111111弄11楼到底哪里");					 
		 
		CreateWd2003.createMmHetongWord2003(data,"",false);//生成 预约打印合同
		
		
	}
}